using Microsoft.SqlServer.Management.Sdk.Sfc;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
	internal class ScriptTableContainer : ScriptObjectContainer
	{
		private List<string> childDropScript = new List<string>();

		private List<string> childCreateScript = new List<string>();

		private List<string> childPermissionScript = new List<string>();

		protected override bool ContainsScriptItems
		{
			get
			{
				return base.ContainsScriptItems || this.childDropScript.Count > 0 || this.childCreateScript.Count > 0 || this.childPermissionScript.Count > 0;
			}
		}

		public ScriptTableContainer(Microsoft.SqlServer.Management.SqlManagerUI.ObjectInfo objectInfo) : base(objectInfo)
		{
		}

		public override void AddFragment(Microsoft.SqlServer.Management.SqlManagerUI.ObjectInfo objectInfo, IEnumerable script, ScriptActions action)
		{
			this.ValidateSource(objectInfo.Urn);
			if (this.IsChild(objectInfo.Urn))
			{
				switch (action)
				{
				case ScriptActions.Permission:
					this.AddChildPermissionScript(script);
					return;
				case ScriptActions.Create:
					this.AddChildCreateScript(script);
					return;
				case ScriptActions.Permission | ScriptActions.Create:
					break;
				case ScriptActions.Drop:
					this.AddChildDropScript(script);
					return;
				default:
					if (action == ScriptActions.Header)
					{
						return;
					}
					break;
				}
				throw new InvalidOperationException();
			}
			base.AddFragment(objectInfo, script, action);
		}

		protected override void AppendCreateScript(ScriptWriter scriptWriter)
		{
			base.AppendCreateScript(scriptWriter);
			base.AppendScriptFragment(scriptWriter, this.childCreateScript);
		}

		protected override void AppendDropScript(ScriptWriter scriptWriter)
		{
			base.AppendScriptFragment(scriptWriter, this.childDropScript);
			base.AppendDropScript(scriptWriter);
		}

		protected override void AppendPermissionScript(ScriptWriter scriptWriter)
		{
			base.AppendPermissionScript(scriptWriter);
			base.AppendScriptFragment(scriptWriter, this.childPermissionScript);
		}

		protected override void ValidateSource(Urn source)
		{
			if (!this.IsChild(source))
			{
				base.ValidateSource(source);
				return;
			}
			if (!this.IsValidChild(source))
			{
				throw new InvalidOperationException();
			}
		}

		private void AddChildCreateScript(IEnumerable script)
		{
			this.childCreateScript.AddRange(ScriptObjectContainer.GetStringArray(script));
		}

		private void AddChildDropScript(IEnumerable script)
		{
			this.childDropScript.AddRange(ScriptObjectContainer.GetStringArray(script));
		}

		private void AddChildPermissionScript(IEnumerable script)
		{
			this.childPermissionScript.AddRange(ScriptObjectContainer.GetStringArray(script));
		}

		private bool IsChild(Urn childItem)
		{
			return !(childItem == this.Key) && childItem.Parent == this.Key;
		}

		private bool IsValidChild(Urn childItem)
		{
			string type;
			return this.IsChild(childItem) && (type = childItem.Type) != null && (type == "ForeignKey" || type == "Check" || type == "FullTextIndex");
		}
	}
}
